package top.yzhelp.enjoyinformation.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.yzhelp.enjoyinformation.controller.res.Result;
import top.yzhelp.enjoyinformation.model.Message;
import top.yzhelp.enjoyinformation.service.MessageService;
import top.yzhelp.enjoyinformation.shiro.ShiroRealm;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 码之泪殇 GitHub: https://github.com/gongsir0630
 * @date 2021/3/26 20:00
 * 你的指尖,拥有改变世界的力量
 * @description 共享信息接口
 */
@RestController
@RequestMapping("msg")
@Slf4j
@RequiresAuthentication
public class MessageController {

    private String getUserId() {
        return ShiroRealm.getUserId();
    }

    @Resource
    private MessageService messageService;

    /**
     * 信息列表
     * @description 支持关键字和分类查询
     * @param page 当前页,默认 1
     * @param size 每页数量,默认 10
     * @param keyword 关键字
     * @param type 分类
     * @return 所有信息
     */
    @GetMapping("list")
    public ResponseEntity<Result<IPage<Message>>> getAllMsg(@RequestParam(required = false,defaultValue = "1") int page,
                                                           @RequestParam(required = false,defaultValue = "10") int size,
                                                           @RequestParam(required = false,defaultValue = "") String keyword,
                                                            @RequestParam(required = false,defaultValue = "") String type) {
        IPage<Message> messagePage = new Page<>(page,size);
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            // 关键字查询
            wrapper.like("content",keyword);
        }
        if (!StringUtils.isEmpty(type)) {
            // 分类查询
            wrapper.eq("type",type);
        }
        // 倒序排序
        wrapper.orderByDesc("id");
        IPage<Message> msgList = messageService.page(messagePage, wrapper);
        return new ResponseEntity<>(Result.success(msgList), HttpStatus.OK);
    }

    /**
     * 根据 id 查看详细信息
     * @param id 信息 id
     * @return 详情信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<Message>> getBookById(@PathVariable("id") int id) {
        return new ResponseEntity<>(Result.success(messageService.getById(id)), HttpStatus.OK);
    }

    /**
     * 修改发布的信息
     * @param id id
     * @param msg 新的信息
     * @return 更新后的信息
     */
    @PostMapping("/{id}")
    public ResponseEntity<Result<Message>> updateBook(@PathVariable("id") int id,
                                                   Message msg) {
        msg.setId(id);
        msg.setPublishTime(new Date());
        messageService.updateById(msg);
        msg = messageService.getById(id);
        return new ResponseEntity<>(Result.success(msg), HttpStatus.OK);
    }

    /**
     * 删除信息
     * @param id id
     * @return 信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Message>> delBookById(@PathVariable("id") int id) {
        Message msg = messageService.getById(id);
        messageService.removeById(id);
        return new ResponseEntity<>(Result.success(msg), HttpStatus.OK);
    }

    /**
     * 发布信息
     * @param msg 信息, 用户 id 不需要
     * @return status
     */
    @PutMapping("")
    public ResponseEntity<Result<String>> saveBook(Message msg) {
        msg.setUserId(getUserId());
        msg.setPublishTime(new Date());
        messageService.save(msg);
        return new ResponseEntity<>(Result.success("save book success"), HttpStatus.OK);
    }
}
